#include "daterestriction.hpp"

#include <QLineEdit>
#include <QIntValidator>
#include <QCalendarWidget>
#include <QMessageBox>

DateRestriction::DateRestriction(int type):RestrictionLayout(type){
	qpbdate=new QPushButton(" /   / ");
	connect((qpbdate),SIGNAL(clicked()),this,SLOT(dateDialog()));
	
	this->addWidget(qlname);
	this->addWidget(qpbdate);
	if(qpbrm)
	this->addWidget(qpbrm);
}

DateRestriction::~DateRestriction(){
	delete qpbdate;
}

void DateRestriction::dateDialog(){
	QMessageBox *datedialog=new QMessageBox();
	QHBoxLayout *qhb=new QHBoxLayout();
	QCalendarWidget *qcw=new QCalendarWidget(datedialog);
	connect(qcw,SIGNAL(activated(const QDate &)),this,SLOT(dateClicked(const QDate &)));
	qhb->addWidget(qcw);
	delete datedialog->layout();
	datedialog->setLayout(qhb);
	datedialog->setWindowTitle("Escolha uma data");
	datedialog->setFixedSize(datedialog->minimumSize());
	datedialog->show();
}

void DateRestriction::dateClicked(const QDate &date){
	((QMessageBox *)(((QWidget*)(sender()))->parent()))->close();
	qpbdate->setText(date.toString("dd/MM/yyyy"));
}

QDate DateRestriction::getDate(){
	return QDate::fromString(qpbdate->text(),"dd/MM/yyyy");
}
